# coding=utf-8
'''
author : 董新强 
createdate : <<创建时间>>
description : <<描述>>
'''

from concurrent.futures import ThreadPoolExecutor
from functools import partial

import tornado
from tornado.ioloop import IOLoop

MAX_THREAD_NUM = 150  # 每个实例的线程数目
TORNADO_VERSION = tornado.version  # tornado的版本
tornado_main_version = int(TORNADO_VERSION[0])  # tornado的主版本

__EXECUTOR__ = ThreadPoolExecutor(MAX_THREAD_NUM)

loop = IOLoop.current()
loop.set_default_executor(__EXECUTOR__)


def _run_async(func, *args, **kwargs):
    return loop.run_in_executor(None, partial(func, *args, **kwargs))


async def _run_async_await(func, *args, **kwargs):
    result = await loop.run_in_executor(None, partial(func, *args, **kwargs))
    return result


def run_async(func, *args, **kwargs):
    if tornado_main_version == 6:
        return _run_async_await(func, *args, **kwargs)
    return _run_async(func, *args, **kwargs)
